<?php
/**
 * Class descript.
 * User: chan
 * Date: 2017/10/26 15:29
 */
namespace app\api\controller;
use think\Request;
use think\Db;
use think\Cache;
use think\Session;
use app\api\model\UserAccount;
use app\api\model\Team;

class Login extends Base
{

    public function login($openid = 1)
    {
        // $input = input();
        // $Oauth2 = new \app\api\service\Oauth2();
        // // $user = [
        // //     'userId' => '1',
        // // ];
        // $user = [
        //     'userId' => $openid,
        // ];
        // $tokenInfo = $Oauth2::generateToken($user);
        // return $this->ajaxReturn($tokenInfo);
    }
    /*
     *  用户注册
     */
    public function register(){

        $input = input();
        if (empty($input['phone'])){
            return $this->ajaxReturn('',-1,'未定义phone');
        }
        if (empty($input['password'])){
            return $this->ajaxReturn('',-1,'未定义password');
        }
        if (empty($input['real_name'])){
            return $this->ajaxReturn('',-1,'未定义real_name');
        }

        if (empty($input['province'])){
            return $this->ajaxReturn('',-1,'未定义province');
        }

        if (empty($input['city'])){
            return $this->ajaxReturn('',-1,'未定义city');
        }

        if (empty($input['wx'])){
            return $this->ajaxReturn('',-1,'未定义wx');
        }

        if (empty($input['nick_name'])){
            return $this->ajaxReturn('',-1,'未定义nick_name');
        }

        $UserAccount = new UserAccount();
        $team = new Team();
        $user = $UserAccount->getUser(['account'=>$input['phone']]);
        if ($user){
            return $this->ajaxReturn('',-1,'该手机号码已注册');
        }

        $code = input('code');
        if (base64_encode($code) != Cache::get('register'.$input['phone'])) {
        return $this->ajaxReturn('',-1,'验证码错误');
        }

        $recommend_id = empty($input['recommend_id']) ? '' : $input['recommend_id'];
        $data = [];
        $data['account'] = $input['phone'];
        $data['password'] = $UserAccount->add_hash($input['password']);
        $data['phone'] = $input['phone'];
        $data['real_name'] = $input['real_name'];
        $data['recommend_id'] = $recommend_id;

        $data['nick_name'] = $input['nick_name'];

        
        $data['province'] = $input['province'];
        $data['city'] = $input['city'];
        $data['county'] = $input['county'];
        $data['wx'] = $input['wx'];

        $data['level'] = 0;
        $data['status'] = 0;
        $data['created_at'] = time();
        $id = $UserAccount->addUser($data);//创建用户

        $teamData = [];
        $teamData['uid'] = $id;
        $teamData['recommend_id'] = $recommend_id;
        $teamData['level'] = 0;
        $teamData['created_at'] = time();
        $tId = $team->addTeam($teamData);//添加团队信息

        $memberData = [];
        $memberData['uid'] = $id;
        $memberData['price'] = 0;
        $memberData['level'] = 0;
        $memberData['recommend_id'] = $recommend_id;
        $memberData['created_at'] = time();
        $mId = db('member_info')->insertGetId($memberData);//添加会员信息

        if ($id){
            $data['id'] = $id;
            return $this->ajaxReturn($data,200,'注册成功');
        }else{
           return $this->ajaxReturn('',-1,'注册失败');
        }
    }
    
  /*
    *   用户登录
    */
   public function userLogin() {
        $input = input();
        if (empty($input['account'])){
            return $this->ajaxReturn('',-1,'未定义account');
        }
        if (empty($input['password'])){
            return $this->ajaxReturn('',-1,'未定义password');
        }
        $UserAccount = new UserAccount();
        $account = $input['account'];
        $password = $input['password'];
        $data = [];
        $res = $UserAccount->getUser(['account'=>$account]);
        if(!$res){
            return $this->ajaxReturn('',-1,'账号错误或帐号不存在！');
        }else{
            $pass_bool = $UserAccount->verification_hash($password,$res['password']);
            if(!$pass_bool){
                return $this->ajaxReturn('',-1,'密码错误');
            }
            // $where = [
            //     'account' => $account,
            //     'password' => $UserAccount->add_hash($password)
            // ];
            $where['account'] = $account;
            // $where['password'] = $UserAccount->add_hash($password);
            $data['login_ip'] = $_SERVER['REMOTE_ADDR'];
            $data['login_time'] = time();
            $userInfo = $UserAccount->getUser($where);
            // if ($userInfo['level'] > 0){//每天刷新
            //     $time = strtotime(date('Y-m-d'));
            //     if ($userInfo['day_time'] < $time) {
            //         $task_num = db('member')->where('level',$userInfo['level'])->value('task_num');//可接任务数量
            //         $data['day_num'] = $task_num;
            //         $data['day_time'] = $time;
            //     }
            // }
            $userInfo = json_decode($res,true);
            db('user_account')->where('account',$account)->update($data);
            $Oauth2 = new \app\api\service\Oauth2();
            $tokenInfo = $Oauth2::generateToken($userInfo);
            return $this->ajaxReturn($tokenInfo);
        }
   }

    /*
     * 忘记密码
     */
    public function forget(){

        $code = input('code');
        $phone = input('phone');
        $account = input('account');
        $root = db('user_account')->where('phone',$phone)->where('account',$account)->find();

        if(!$root) return $this->ajaxReturn('',-1,'账号或手机不存在');
        if($root['status'] == 1) return $this->ajaxReturn('',-1,'账号禁止登陆,请联系客服!');
        if(empty(input('password'))) return $this->ajaxReturn('',-1,'密码不能为空!');

        if (base64_encode($code) != Cache::get('forget'.$phone)) {
        return $this->ajaxReturn('',-1,'验证码错误');
        }

        $update_date['password'] = password_hash(input('password'),PASSWORD_DEFAULT);

        $result = Model('user_account')->where('id',$root['id'])->update($update_date);

        if($result['bool'] == 'false')return $this->ajaxReturn('',-1,'修改失败!');
        return $this->ajaxReturn('',1,'修改成功!');
    }

    /*
     *  刷新令牌
     */
    public function  reToken($refresh_token,$access_token){
        $Oauth2 = new \app\api\service\Oauth2();
        $tokenInfo = $Oauth2->refreshToken($refresh_token,$access_token);
        return $this->ajaxReturn($tokenInfo);
    }

/*    public function login_html(){

        return 
    }*/

}